import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import first from '@/views/firstView.vue'
import { Message} from 'element-ui'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) { 
  return originalPush.call(this, location).catch(err => err)
};

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children:[
      {
        path:"index",
        name:"index",
        component:first,
      },
      {
        path:"web",
        name:"web",
        component:()=> import("@/views/webView.vue"),
        children:[
          {
            path:"database",
            name:"database",
            component:()=> import("@/components/dataBase.vue")
          },
          // {
          //   path:"redirect",
          //   name:"redirect",
          //   redirect: 'database', //重定向
          // }
        ]
      },
      {
        path:"manage",
        name:"manage",
        component:()=> import("@/views/manageView.vue")
      }
    ],
  },
  // {
  //   path: '/web',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(async (to, from,next) => {
  const aa=JSON.parse(localStorage.getItem("login")).power

  if(aa!=""||aa!=null){
    if (aa<2&&to.name == 'index'||aa<2&&to.name== 'manage') {
      Message.error('无法访问此页面,您没有权限');
      // 将用户重定向到登录页面
      next({path:"/home"})
    }else {
      next()
    }
  }else{
    next({path:"/home"})
  }
})
export default router
